<template>
  <div class="tab-control">
    <div
      class="tab-control-item"
      v-for="(item, index) in titles"
      :key="index"
      :class="{active : index === currentIndex}"
      @click="itemClick(index)"
    >
      <span>{{item}}</span>
    </div>
  </div>
</template>

<script>
export default {
  name: "tab-control",
  props: {
    titles: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  mounted() {
    // window.addEventListener("load", () => {
    //   let tab = document.querySelector(".tab-control");
    //   let top = tab.offsetTop;
    //   window.addEventListener("scroll", () => {
    //     let top2 = document.documentElement.scrollTop;
    //     if (top2 >= top) {
    //       tab.classList.add("tab-control-fixed");
    //     } else {
    //       tab.classList.remove("tab-control-fixed");
    //     }
    //   });
    // });
  },
  data() {
    return {
      currentIndex: 0
    };
  },
  methods: {
    itemClick(index) {
      this.currentIndex = index;
      this.$emit("tabclick", index);
    }
  }
};
</script>

<style scoped>
.tab-control {
  display: flex;
  height: 40px;
  line-height: 40px;
  text-align: center;
  background: #fff;
}
.tab-control-item {
  flex: 1;
  cursor: pointer;
}
.tab-control-item span {
  padding: 3px 5px;
}
.tab-control-item.active {
  color: var(--color-high-text);
}
.tab-control-item.active span {
  border-bottom: 2px solid var(--color-tint);
}
</style>